<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2006/09/23 01:50:30 $)
// $RCSfile: functions_clientnews.php,v $ : $Revision: 1.3 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

define("FILTER_TODAY", "today");
define("FILTER_THISWEEK", "thisweek");
define("FILTER_LASTWEEK", "lastweek");
define("FILTER_THISMONTH", "thismonth");
define("FILTER_LASTMONTH", "lastmonth");
define("FILTER_THISYEAR", "thisyear");

/**
* Fetch the News for Client Interface
*/
function getClientNews($filtertype, $_offset, $_limit)
{
	global $dbCore, $_SWIFT, $template;

	if ($filtertype == FILTER_TODAY)
	{
		$datethreshold = mktime(0,0,0,date("m"), date("d"), date("Y"));
	} else if ($filtertype == FILTER_THISWEEK) {
		$today = mktime(0,0,0,date("m"), date("d"), date("Y"));
		$daythresh = 86400*date("w");
		$datethreshold = $today-$daythres;
	} else if ($filtertype == FILTER_LASTWEEK) {
		$today = mktime(0,0,0,date("m"), date("d"), date("Y"));
		$daythresh = 86400*date("w");
		$datethreshold = $today-$daythres-604800;
	} else if ($filtertype == FILTER_THISMONTH) {
		$datethreshold = mktime(0,0,0,date("m"), "1", date("Y"));
	} else if ($filtertype == FILTER_LASTMONTH) {
		$datethreshold = mktime(0,0,0,getLastMonth(date("m")), "1", date("Y"));
	} else if ($filtertype == FILTER_THISYEAR) {
		$datethreshold = mktime(0,0,0,"1", "1", date("Y"));
	}

	$_newscount = $dbCore->queryFetch("SELECT COUNT(*) AS totalitems FROM `". TABLE_PREFIX ."news` WHERE `tgroupid` = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' AND `newstype` = '". SWIFTPUBLIC ."' AND (`expiry` > '". DATENOW ."' OR `expiry` = '0')". iif(!empty($datethreshold), " AND `dateline` >= '". intval($datethreshold) ."'").";");
	if (empty($_newscount["totalitems"]))
	{
		$_totalitems = 0;
	} else {
		$_totalitems = intval($_newscount["totalitems"]);
	}
	//Mahesh Slaria...... $thres = $_offset*$_limit
	$thres = $_offset+$_limit;

	if ((($thres+1) < $_totalitems) && ($_totalitems > $_limit))
	{
		$template->assign("nextpage", true);
		$template->assign("nextoffset", ($_offset+$_limit));
	}
	if ($_offset > 0)
	{
		$template->assign("prevpage", true);
		$template->assign("prevoffset", ($_offset-$_limit));
	}

	$newslist = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."news` WHERE `tgroupid` = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' AND `newstype` = '". SWIFTPUBLIC ."' AND (`expiry` > '". DATENOW ."' OR `expiry` = '0')". iif(!empty($datethreshold), " AND `dateline` >= '". intval($datethreshold) ."'")." ORDER BY `newsid` DESC LIMIT ". intval($_offset) .", ". intval($_limit) .";");
	while ($dbCore->nextRecord())
	{
		$newslist[$dbCore->Record["newsid"]] = $dbCore->Record;
		$newslist[$dbCore->Record["newsid"]]["description"] = getProcessedHTML($dbCore->Record["description"]);
		$newslist[$dbCore->Record["newsid"]]["dateline"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $dbCore->Record["dateline"]);
		$newslist[$dbCore->Record["newsid"]]["fullname"] = $_SWIFT["staffcache"][$dbCore->Record["staffid"]]["fullname"];
		if ($dbCore->Record["edited"] == 1)
		{
			$newslist[$dbCore->Record["newsid"]]["editeddateline"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $dbCore->Record["editeddateline"]);
		}
	}

	return $newslist;
}

/**
* Get the last month
*/
function getLastMonth($month)
{
	if ($month == 1)
	{
		return 12;
	} else {
		return $month-1;
	}
}
?>